package iglesia.invimosa.lyrics.manager;

import iglesia.invimosa.lyrics.manager.datalayer.LyricsManagerData;

import java.awt.BorderLayout;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
//import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.Point;
import javax.swing.JButton;
import java.awt.ComponentOrientation;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.JEditorPane;
import java.awt.GridBagConstraints;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.border.EtchedBorder;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class InsertNewSongs extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JPanel jPanel = null;

	private JLabel jLabel = null;

	private JTextField jSongName = null;

	private JPanel jPanel1 = null;

	private JPanel jPanel2 = null;

	private JPanel jPanel3 = null;

	private JButton jButton = null;

	private JButton jButton1 = null;

	private JEditorPane jEditorPane = null;

	private JLabel jLabel1 = null;

	private JComboBox jArtistBox = null;

	private JButton jNewArtistButton = null;

	private JPanel jPanel4 = null;

	private JLabel jLabel2 = null;

	private JComboBox jGenreBox = null;

	private JButton jButton3 = null;

	private JLabel jLabel3 = null;

	private LyricsManagerData transactions = null;  //  @jve:decl-index=0:
	
	private String dbname;  //  @jve:decl-index=0:
	private String username;  //  @jve:decl-index=0:
	private String password;  //  @jve:decl-index=0:
	
	private SAXBuilder builder = null;
	private Document document = null;
	private Element root = null;
	
	/**
	 * This is the default constructor
	 */
	public InsertNewSongs() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(675, 709);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("INSERTAR NUEVAS CANCIONES");
		this.setLocationRelativeTo(null);
				
		builder = new SAXBuilder();
		
		try {
			document = builder.build(new File("config.xml"));
			root = document.getRootElement();
			
			this.dbname = root.getChild("DatabaseCredentials").getChild("server").getValue();
			this.username = root.getChild("DatabaseCredentials").getChild("userid").getValue();
			this.password = root.getChild("DatabaseCredentials").getChild("password").getValue();
			
		} catch (JDOMException e) {
			JOptionPane.showMessageDialog( null, "JDOMException", e.getMessage(), JOptionPane.ERROR_MESSAGE );
			System.exit(0);
		} catch (IOException e) {
			JOptionPane.showMessageDialog( null, "IOException", e.getMessage(), JOptionPane.ERROR_MESSAGE );
			System.exit(0);
		}
		
		transactions = new LyricsManagerData(dbname, username, password);
		ResultSet rs = transactions.getListOfArtists();
		
		try {
			updateJComboBox(jArtistBox, rs);
		} catch (SQLException e) {
			JOptionPane.showMessageDialog( null, "SQLException", e.getMessage(), JOptionPane.ERROR_MESSAGE );
		}
		
		rs = transactions.getAllSongTypes();
		
		try {
			updateJComboBox(jGenreBox, rs);
		} catch (SQLException e) {
			JOptionPane.showMessageDialog( null, "SQLException", e.getMessage(), JOptionPane.ERROR_MESSAGE );
		}
	}
	
	public void updateJComboBox(JComboBox cbox, ResultSet rs) throws SQLException {
		while(rs.next()){
			   cbox.addItem(rs.getString(2).toUpperCase());
	    }
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanel(), null);
			jContentPane.add(getJPanel1(), null);
			jContentPane.add(getJPanel2(), null);
			jContentPane.add(getJPanel3(), null);
			jContentPane.add(getJPanel4(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel = new JLabel();
			jLabel.setText("NOMBRE: ");
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.setSize(new Dimension(633, 18));
			jPanel.setLocation(new Point(16, 15));
			jPanel.add(jLabel, BorderLayout.WEST);
			jPanel.add(getJTextField(), BorderLayout.CENTER);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jSongName == null) {
			jSongName = new JTextField();
		}
		return jSongName;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("ARTISTA: ");
			jPanel1 = new JPanel();
			jPanel1.setLayout(new BorderLayout());
			jPanel1.setBounds(new Rectangle(16, 46, 328, 18));
			jPanel1.setPreferredSize(new Dimension(0, 0));
			jPanel1.add(jLabel1, BorderLayout.WEST);
			jPanel1.add(JGetArtistsComboBox(), BorderLayout.CENTER);
			jPanel1.add(getJNewArtistButton(), BorderLayout.EAST);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.gridy = 0;
			jLabel3 = new JLabel();
			jLabel3.setText("LETRAS DE LA CANCION");
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = 1;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridx = 0;
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.setSize(new Dimension(631, 541));
			jPanel2.setLocation(new Point(16, 76));
			jPanel2.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
			jPanel2.add(getJEditorPane(), gridBagConstraints);
			jPanel2.add(jLabel3, gridBagConstraints1);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			jPanel3 = new JPanel();
			jPanel3.setLayout(new FlowLayout());
			jPanel3.setSize(new Dimension(631, 42));
			jPanel3.setLocation(new Point(16, 625));
			jPanel3.setComponentOrientation(ComponentOrientation.UNKNOWN);
			jPanel3.setBorder(BorderFactory.createTitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			jPanel3.add(getJButton(), null);
			jPanel3.add(getJButton1(), null);
			jPanel3.add(getJButton3(), null);
		}
		return jPanel3;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Guardar");
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				@Override
				public void mouseClicked(java.awt.event.MouseEvent e) {
					String name = getJTextField().getText();
					String lyrics = getJEditorPane().getText();
					int songType = JGetArtistsComboBox().getSelectedIndex();
					int artist = JGetGenreComboBox().getSelectedIndex();
					
					if(name.equalsIgnoreCase("") | lyrics.equalsIgnoreCase(""))
					{
						JOptionPane.showMessageDialog(jContentPane, "Campos requeridos aun no han sido llenados", 
								"NUEVA CANCION", JOptionPane.ERROR_MESSAGE);
					}else{
						JOptionPane.showMessageDialog(jContentPane, "Cancion Guardada con Exito", 
								"NUEVA CANCION", JOptionPane.INFORMATION_MESSAGE);
					}
					//ArrayList<String> tags = new ArrayList<String>();
					//boolean results = transactions.insertNewSong(name, songType, artist, lyrics, tags);
					
					
					//if(results){
						
					//}
					//getJTextField().requestFocusInWindow();
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Limpiar");
			jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
				@Override
				public void mouseClicked(java.awt.event.MouseEvent e) {
					getJTextField().setText("");
					getJEditorPane().setText("");
					getJTextField().requestFocusInWindow();
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jEditorPane	
	 * 	
	 * @return javax.swing.JEditorPane	
	 */
	private JEditorPane getJEditorPane() {
		if (jEditorPane == null) {
			jEditorPane = new JEditorPane();
		}
		return jEditorPane;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox JGetArtistsComboBox() {
		if (jArtistBox == null) {
			jArtistBox = new JComboBox();
			jArtistBox.setEditable(true);
			
		}
		return jArtistBox;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJNewArtistButton() {
		if (jNewArtistButton == null) {
			jNewArtistButton = new JButton();
			jNewArtistButton.setText("Nuevo Artista");
			jNewArtistButton.addMouseListener(new java.awt.event.MouseAdapter() {
				@Override
				public void mouseClicked(java.awt.event.MouseEvent e) {
					String artistName = JGetArtistsComboBox().getSelectedItem().toString();
					int id = transactions.insertArtist(artistName);
					
					if (id > 0)
						JOptionPane.showMessageDialog(jContentPane, "Artista Agregado con Exito", 
								"NUEVO ARTISTA", JOptionPane.INFORMATION_MESSAGE);
					
					else{
						JOptionPane.showMessageDialog(null, "El Artista no pudo ser agregado", 
								"NUEVO ARTISTA", JOptionPane.ERROR_MESSAGE);
					}
					
					ResultSet rs = transactions.getListOfArtists();
					
					try {
						updateJComboBox(jArtistBox, rs);
					} catch (SQLException ex) {
						JOptionPane.showMessageDialog( null, "SQLException", ex.getMessage(), JOptionPane.ERROR_MESSAGE );
					}
				}
			});
		}
		return jNewArtistButton;
	}

	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("GENERO: ");
			jPanel4 = new JPanel();
			jPanel4.setLayout(new BorderLayout());
			jPanel4.setBounds(new Rectangle(360, 46, 287, 18));
			jPanel4.add(jLabel2, BorderLayout.WEST);
			jPanel4.add(JGetGenreComboBox(), BorderLayout.CENTER);
		}
		return jPanel4;
	}

	/**
	 * This method initializes jComboBox1	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox JGetGenreComboBox() {
		if (jGenreBox == null) {
			jGenreBox = new JComboBox();
		}
		return jGenreBox;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText("Cerrar");
			jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
				@Override
				public void mouseClicked(java.awt.event.MouseEvent e) {
					setVisible(false);
				}
			});
		}
		return jButton3;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
